package com.zhitong.money.mapper;

import com.zhitong.money.entity.Menu;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface MenuMapper {


    @Results(id = "menuMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "parentId",property = "parentId"),
            @Result(column = "url",property = "url"),
            @Result(column = "icon",property = "icon"),
            @Result(column = "level",property = "level"),
            @Result(column = "status",property = "status"),
            @Result(column = "description",property = "description")
    })
    @Select("select * from menu")
    public List<Menu> findAllMenu();

    @ResultMap("menuMap")
    @Select({
            "<script>",
            "select * from menu",
            "<where>",
            "<if test='id!=null and id != \"\"'>and id=#{id}</if>",
            "<if test='name!=null and name!=\"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='parentId!=null and parentId != \"\"'>and parentId=#{parentId}</if>",
            "<if test='level!=null and level != \"\"'>and level=#{level}</if>",
            "<if test='status!=null and status != \"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<Menu> findMenuByMap(Map<String,Object> map);

    @ResultMap("menuMap")
    @Select("select * from menu where id in (select mid from role_menu where rid = #{rid})")
    public List<Menu> findMenuByRid(String rid);

    @ResultMap("menuMap")
    @Select("select * from menu where id = #{id}")
    public Menu findMenuById(String id);

    @ResultMap("menuMap")
    @Select("select * from menu where name=#{name}")
    public Menu findMenuByName(String name);

    @ResultMap("menuMap")
    @Select("select * from menu where parentId = #{parentId}")
    public List<Menu> findMenuByParentId(String parentId);

    @Update({
            "<script>",
            "update menu",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='name!=null and name!=\"\"'> name=#{name},</if>",
            "<if test='parentId!=null and parentId!=\"\"'> parentId=#{parentId},</if>",
            "<if test='url!=null and url !=\"\"'> url=#{url},</if>",
            "<if test='icon!=null and icon!=\"\"'> icon=#{icon},</if>",
            "<if test='level!=null and level!=\"\"'> level=#{level},</if>",
            "<if test='status!=null and status!=\"\"'> status=#{status},</if>",
            "<if test='description!=null and description!=\"\"'> description=#{description},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updateMenu(Menu menu);

    @Insert("insert into menu values(#{id},#{name},#{parentId},#{url},#{icon},#{level},#{status},#{description})")
    public void addMenu(Menu menu);

    @Delete("delete from menu where id=#{id}")
    public int delMenu(String id);

    @Delete({
            "<script>",
            "delete from menu",
            "where id in",
            "<foreach item='item' collection='ids' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public int delMenus(List<String> ids);

}
